(function() {

	var appModule = angular.module('tracecashModule');

	appModule.directive('agreementList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/search-sinister/agreement-list.html',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$http.get($rootScope.REST_PATH + '/agreement/findAll').success(function(res) {
					$scope.agreements = res;
				});
			}],
			require : '^ngModel',
			scope : {
				ngModel : '='
			}
		};
	});

	appModule.directive('warrantyList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/search-sinister/warranty-list.html',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$http.get($rootScope.REST_PATH + '/warrantyType/findAll').success(function(res) {
					$scope.warranties = res;
				});
			}],
			require : '^ngModel',
			scope : {
				warrantyModel : '=ngModel'
			}
		};
	});

	appModule.directive('stateList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/search-sinister/state-list.html',
			controller : ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
				$http.get($rootScope.REST_PATH + '/sinisterEnum/SinisterState').success(function(result) {
					$scope.states = result;
				});
			}],
			require : '^ngModel',
			scope : {
				stateModel : '=ngModel'
			}
		};
	});

	appModule.directive('searchResultList', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/search-sinister/search-sinister-list.html'
		};
	});
	
	appModule.controller('SinisterSearchCtrl', ['$scope', '$rootScope', '$http', function($scope, $rootScope, $http) {
		$scope.initial = angular.copy($scope.sinisterSearchAction);
		$scope.clear = function() {
			angular.copy($scope.initial, $scope.sinisterSearchAction);
			$scope.form.$setPristine();
		};
		$scope.search = function() {
			$scope.setPage(1);
		};
		$scope.setPage = function(page) {
			$scope.currentPage = page;
			$http.post($rootScope.REST_PATH + '/sinister/search', $scope.sinisterSearchAction, {
				params : {
					p : $scope.currentPage,
					n : $rootScope.PAGE_SIZE
				}
			}).success(function(data) {
				$scope.searchResults = data;
			});
		};
	}]);
	
})();